﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Maticsoft.DBUtility;
using System.Data.SqlClient;


namespace ElingOA6_Admin_DSJCJ.DAL
{
    public class SysPhrase
    {

        public SysPhrase()
        { }
        #region Method

        #endregion

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string FPhraseIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_SysPhrase ");
            strSql.Append(" where FPhraseID in (" + FPhraseIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        #region  增加的方法
        /// <summary>
        /// 获得数据列表
        /// <creator>wangqy</creator>
        /// <date>2013-7-13</date>
        /// </summary>
        public DataSet GetPhraseManagementListByView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM v_SysPhrase ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="strWhere"></param>
        /// <param name="fOrderFiled"></param>
        /// <returns></returns>
        public DataSet GetPhraseManagementListByPage(string tableName, string fields, int pageSize, int pageIndex, string strWhere, string fOrderFiled, int orderType, out int total)
        {
            SqlParameter[] parameters = {  
                    new SqlParameter("@tableName", SqlDbType.VarChar, 50),  
                    new SqlParameter("@fields", SqlDbType.VarChar, 2000),  
                    new SqlParameter("@orderField", SqlDbType.VarChar, 500),  
                    new SqlParameter("@sqlWhere", SqlDbType.VarChar,2000),  
                    new SqlParameter("@pageSize", SqlDbType.Int),  
                    new SqlParameter("@pageIndex", SqlDbType.Int),  
                    new SqlParameter("@orderType", SqlDbType.Int),  
                    new SqlParameter("@TotalPage", SqlDbType.Int),
                    new SqlParameter("@TotalRecordCount", SqlDbType.Int)  
                };

            parameters[0].Value = tableName;
            parameters[1].Value = fields;
            parameters[2].Value = fOrderFiled;
            parameters[3].Value = strWhere;
            parameters[4].Value = pageSize;
            parameters[5].Value = pageIndex;
            parameters[6].Value = orderType;
            parameters[7].Direction = ParameterDirection.Output;
            parameters[8].Direction = ParameterDirection.Output;
            DataSet ds = DbHelperSQL.RunProcedure("up_GetRecordByPage", parameters, "ds", 60 * 30);
            total = int.Parse(parameters[8].Value.ToString());
            return ds;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public decimal Add(Model.SysPhrase model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_SysPhrase(");
            strSql.Append("FEntityNo,FPhrase,FURL,FPubDate,FPubIp,FEmplNo,FDeptNo,FPhraseNo,FIsTop)");
            strSql.Append(" values (");
            strSql.Append("@FEntityNo,@FPhrase,@FURL,@FPubDate,@FPubIp,@FEmplNo,@FDeptNo,@FPhraseNo,@FIsTop)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FPhrase", SqlDbType.NVarChar,255),
					new SqlParameter("@FURL", SqlDbType.NVarChar,255),
					new SqlParameter("@FPubDate", SqlDbType.DateTime),
					new SqlParameter("@FPubIp", SqlDbType.VarChar,15),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FPhraseNo", SqlDbType.NVarChar,20),
					new SqlParameter("@FIsTop", SqlDbType.Int,4)};
            parameters[0].Value = model.FEntityNo;
            parameters[1].Value = model.FPhrase;
            parameters[2].Value = model.FURL;
            parameters[3].Value = model.FPubDate;
            parameters[4].Value = model.FPubIp;
            parameters[5].Value = model.FEmplNo;
            parameters[6].Value = model.FDeptNo;
            parameters[7].Value = model.FPhraseNo;
            parameters[8].Value = model.FIsTop;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToDecimal(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.SysPhrase model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_SysPhrase set ");
            strSql.Append("FPhrase=@FPhrase,");
            strSql.Append("FURL=@FURL,");
            strSql.Append("FPubDate=@FPubDate,");
            strSql.Append("FPubIp=@FPubIp,");
            strSql.Append("FEmplNo=@FEmplNo,");
            strSql.Append("FDeptNo=@FDeptNo,");
            strSql.Append("FPhraseNo=@FPhraseNo,");
            strSql.Append("FIsTop=@FIsTop");
            strSql.Append(" where FPhraseID=@FPhraseID");
            SqlParameter[] parameters = {
					new SqlParameter("@FPhrase", SqlDbType.NVarChar,255),
					new SqlParameter("@FURL", SqlDbType.NVarChar,255),
					new SqlParameter("@FPubDate", SqlDbType.DateTime),
					new SqlParameter("@FPubIp", SqlDbType.VarChar,15),
					new SqlParameter("@FEmplNo", SqlDbType.NVarChar,15),
					new SqlParameter("@FDeptNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FPhraseNo", SqlDbType.NVarChar,20),
					new SqlParameter("@FIsTop", SqlDbType.Int,4),
					new SqlParameter("@FPhraseID", SqlDbType.Decimal,9),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12)};
            parameters[0].Value = model.FPhrase;
            parameters[1].Value = model.FURL;
            parameters[2].Value = model.FPubDate;
            parameters[3].Value = model.FPubIp;
            parameters[4].Value = model.FEmplNo;
            parameters[5].Value = model.FDeptNo;
            parameters[6].Value = model.FPhraseNo;
            parameters[7].Value = model.FIsTop;
            parameters[8].Value = model.FPhraseID;
            parameters[9].Value = model.FEntityNo;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FPhraseID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_SysPhrase ");
            strSql.Append(" where FPhraseID=@FPhraseID");
            SqlParameter[] parameters = {
					new SqlParameter("@FPhraseID", SqlDbType.Decimal)
};
            parameters[0].Value = FPhraseID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string FEntityNo, decimal FPhraseID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_SysPhrase ");
            strSql.Append(" where FEntityNo=@FEntityNo and FPhraseID=@FPhraseID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,50),
					new SqlParameter("@FPhraseID", SqlDbType.Decimal)};
            parameters[0].Value = FEntityNo;
            parameters[1].Value = FPhraseID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.SysPhrase GetModel(decimal FPhraseID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FPhraseID,FEntityNo,FPhrase,FURL,FPubDate,FPubIp,FEmplNo,FDeptNo,FPhraseNo,FIsTop from t_SysPhrase ");
            strSql.Append(" where FPhraseID=@FPhraseID");
            SqlParameter[] parameters = {
					new SqlParameter("@FPhraseID", SqlDbType.Decimal)
};
            parameters[0].Value = FPhraseID;

            Model.SysPhrase model = new Model.SysPhrase();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["FPhraseID"].ToString() != "")
                {
                    model.FPhraseID = decimal.Parse(ds.Tables[0].Rows[0]["FPhraseID"].ToString());
                }
                model.FEntityNo = ds.Tables[0].Rows[0]["FEntityNo"].ToString();
                model.FPhrase = ds.Tables[0].Rows[0]["FPhrase"].ToString();
                model.FURL = ds.Tables[0].Rows[0]["FURL"].ToString();
                if (ds.Tables[0].Rows[0]["FPubDate"].ToString() != "")
                {
                    model.FPubDate = DateTime.Parse(ds.Tables[0].Rows[0]["FPubDate"].ToString());
                }
                model.FPubIp = ds.Tables[0].Rows[0]["FPubIp"].ToString();
                model.FEmplNo = ds.Tables[0].Rows[0]["FEmplNo"].ToString();
                model.FDeptNo = ds.Tables[0].Rows[0]["FDeptNo"].ToString();
                model.FPhraseNo = ds.Tables[0].Rows[0]["FPhraseNo"].ToString();
                if (ds.Tables[0].Rows[0]["FIsTop"].ToString() != "")
                {
                    model.FIsTop = int.Parse(ds.Tables[0].Rows[0]["FIsTop"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取条数
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int getCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from v_SysPhrase ");
            if (strWhere != "")
            {
                strSql.Append(" where " + strWhere);
            }

            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (null != obj)
            {
                return int.Parse(obj.ToString());
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取最新短句 部分字段 FPhrase,FURL
        /// </summary>
        /// <param name="entityNo"></param>
        /// <returns></returns>
        public Model.SysPhrase GetModelPart(string entityNo)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FPhrase,FURL from t_SysPhrase ");
            strSql.Append(" where FEntityNo=@FEntityNo");
            strSql.Append(" order by FIsTop desc,FPhraseID desc");
            SqlParameter[] parameters = { 
                    new SqlParameter("@FEntityNo", SqlDbType.NVarChar, 15) };
            parameters[0].Value = entityNo;

            Model.SysPhrase model = new Model.SysPhrase();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["FPhrase"] != null && ds.Tables[0].Rows[0]["FPhrase"].ToString() != "")
                {
                    model.FPhrase = ds.Tables[0].Rows[0]["FPhrase"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FURL"] != null && ds.Tables[0].Rows[0]["FURL"].ToString() != "")
                {
                    model.FURL = ds.Tables[0].Rows[0]["FURL"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }

        }


        #endregion



    }
}